﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComputerArchitecture.Core;
using ComputerArchitecture.Core.Logging;
using ComputerArchitecture.Numbers;
using ComputerArchitecture.Units.Integers.Signed.Interfaces;

namespace ComputerArchitecture.Units.Integers.Signed.Implementations
{
    public class SignedCompareUnit : LoggingObject, ISignedCompareUnit
    {
        #region Fields

        private readonly ISignedSubstractUnit _substractUnit;

        #endregion

        public SignedCompareUnit(ILoggingService loggingService, ISignedSubstractUnit substractUnit) 
            : base(loggingService)
        {
            ArgumentValidator.NullValidate(substractUnit, "substractUnit");
            _substractUnit = substractUnit;
        }

        public SignedCompareUnit(ILoggingService loggingService)
            : this(loggingService, new SignedSubstractUnit(loggingService))
        {

        }

        int ICompareUnit.Compare(IBitNumber first, IBitNumber second)
        {
            return this.Compare((ISignedBitNumber) first, (ISignedBitNumber) second);
        }

        public int Compare(ISignedBitNumber first, ISignedBitNumber second)
        {
            ArgumentValidator.NullValidate(first);
            ArgumentValidator.NullValidate(second);
            bool overflow;
            var result = _substractUnit.Substract(first, second, out overflow);
            return result.IsZero ? 0 : result.Sign;
        }
    }
}
